<template>
	<view v-if="goodInfo.goods_name" class="good_container">
		<swiper :circular="true" :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000">
			<swiper-item v-for="(item, index) in goodInfo.pics" :key="index">
				<image :src="item.pics_big_url || defaultPic" class="swiper-item"></image>
			</swiper-item>
		</swiper>
		<view class="good_info_box">
			<text class="good_price">￥{{goodInfo.goods_price}}</text>
			<view class="good_info">
				<text class="good_name">{{goodInfo.goods_name}}</text>
				<view class="collect">
					<uni-icons type="star" size="18" color="gray"></uni-icons>
					<text>收藏</text>
				</view>
			</view>
			<view class="express">
				<text>快递：免运费</text>
			</view>
		</view>
		<view class="richText">
			<rich-text :nodes="goodInfo.goods_introduce"></rich-text>
		</view>
		<uni-goods-nav :fill="true"  :options="options" :buttonGroup="buttonGroup" @click="onClick" @buttonClick="buttonClick"></uni-goods-nav>
	</view>
</template>

<script>
	import { mapState } from 'vuex'
	import { mapMutations, mapGetters } from 'vuex'
	export default {
		data() {
			return {
				goodInfo: {},
				defaultPic: '../../static/api.png',
				options: [{
				            icon: 'headphones',
				            text: '客服'
				        }, {
				            icon: 'shop',
				            text: '店铺',
				            info: 2,
				            infoBackgroundColor:'#007aff',
				            infoColor:"red"
				        }, {
				            icon: 'cart',
				            text: '购物车',
				            info: 2
				        }],
				        buttonGroup: [{
				          text: '加入购物车',
				          backgroundColor: '#ff0000',
				          color: '#fff'
				        },
				        {
				          text: '立即购买',
				          backgroundColor: '#ffa200',
				          color: '#fff'
				        }
				        ]
			};
		},
		onLoad: function(options) {
			this.getGoodsInfo(options.goods_id)
		},
		methods: {
			...mapMutations('m_cart', ['addToCart']),
			async getGoodsInfo(id) {
				const { data } = await uni.$http.get('/api/public/v1/goods/detail', {
					goods_id: id
				})
				console.log(data)
				data.message.goods_introduce = data.message.goods_introduce.replace(/<img /g, '<img style="display:block;" ')
				this.goodInfo = data.message
			},
			// 右侧点击事件
			buttonClick(e) {
				if (e.content.text === '加入购物车') {
					this.addToCart({ 
						goods_id: this.goodInfo.goods_id, 
						goods_name: this.goodInfo.goods_name, 
						goods_price: this.goodInfo.goods_price, 
						goods_count: 1,
						goods_small_logo: this.goodInfo.goods_small_logo, 
						goods_state: false,
					})
				};
			},
			// 左侧点击事件
			onClick(e) {
				if (e.content.text === '购物车') {
					uni.switchTab({
						url: "/pages/cart/cart"
					})
				}
			}
		},
		computed: {
			...mapState('m_cart', ['cart']),
			...mapGetters('m_cart', ['total']),
		},
		watch: {
			total: {
				handler(newValue) {
					this.options[2].info = newValue
				},
				immediate: true
			}
		}
	}
</script>

<style lang="scss">
uni-goods-nav {
	position: fixed;
	bottom: 0;
	left: 0;
	height: 50px;
	width: 100%;
}
.good_container {
	padding-bottom: 50px;
}
swiper {
	height: 750rpx;
	image {
		width: 100%;
		height: 100%;
	}
}
.good_info_box {
	padding: 18px;
	height: 150rpx;
	display: flex;
	flex-direction: column;
	justify-content: space-between;
	margin-bottom: 20px;
	.good_price {
	}
	.good_info {
		display: flex;
		justify-content: space-between;
		margin-bottom: 10px;
		.good_name{
			
		}
		.collect{
			width: 120px;
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;
			color:#C0C0C0
		}
	}
	.express {
		text {
			color:#C0C0C0;
			font-size: 14px;
		}
	}
}
</style>
